package cn.samler.diandi.api.service.impl;

import cn.samler.diandi.api.entity.User;
import cn.samler.diandi.api.entity.UserResume;
import cn.samler.diandi.api.mapper.ResumeMapper;
import cn.samler.diandi.api.model.io.Response;
import cn.samler.diandi.api.service.ResumeService;
import cn.samler.diandi.api.utils.ContextHolderUtil;
import cn.samler.diandi.api.utils.ResponseBuilder;
import org.springframework.stereotype.Service;

@Service
public class ResumeServiceImpl implements ResumeService {
    private final ResumeMapper resumeMapper;

    public ResumeServiceImpl(ResumeMapper resumeMapper) {
        this.resumeMapper = resumeMapper;
    }

    /**
     * 查看用户个人资料
     *
     * @param uid user id
     * @return Response
     */
    @Override
    public Response detail(Integer uid) {
        UserResume resume;
        if(uid <= 0) {
            User user = (User) ContextHolderUtil.getCurrentUser();
            uid = user.getId();
        }
        resume = resumeMapper.detail(uid);

        return new ResponseBuilder().success().msg("获取成功").data(resume).build();
    }

    /**
     * 更新个人资料
     *
     * @param form 个人资料表单
     * @return Response
     */
    @Override
    public Response update(UserResume form) {
        User user = (User) ContextHolderUtil.getCurrentUser();
        form.setId(user.getId());

        if(resumeMapper.update(form) > 0)
            return new ResponseBuilder().success().msg("更新成功").build();

        return new ResponseBuilder().failure().msg("更新失败").build();
    }
}
